package pers.xiaoliang.concurrency.example.concurrent;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @version v1.0
 * @description: 使用FutureTask
 * @author: xiaoliang.liu
 * @date: 2018/7/29 9:07
 */
@Slf4j
public class FutureExample2 {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 创建futrueTask实例,并在参数里写任务
        FutureTask<String> stringFutureTask = new FutureTask<>(() -> {
            log.info("do something int callable");
            Thread.sleep(3000);
            return "done";
        });

        // 获得线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.submit(stringFutureTask);
        log.info("do something in main");
        Thread.sleep(1000);
        String result = stringFutureTask.get();
        log.info("result : {}", result);
    }
}
